package demo.db.zookeeper;

import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;

import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.KeeperState;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

public class CreateGroup implements Watcher {

    private static final int SESSION_TIMEOUT = 5000;
    // ZooKeeper类是客户端API的主要类，用于维护客户端和ZooKeeper服务之间的连接
    private ZooKeeper zk;
    // 锁存器(latch)此计数器为1，表示在释放所有等待线程之前需要发生的事件数，
    private CountDownLatch connectedSignal = new CountDownLatch(1);

    public void connect(String host) throws IOException, InterruptedException {
        // 参数this表示一个Watcher对象接收来自于Zookeeper的回调，以获得各种事件的通知，在此表示CreateGroup对象
        zk = new ZooKeeper(host, SESSION_TIMEOUT, this);
        connectedSignal.await();
    }

    @Override
    public void process(WatchedEvent event) {
        if (KeeperState.SyncConnected == event.getState()) {
            // 在调用这个方法表示计数器递减1，若计数器的值变为0，则await()方法返回
            connectedSignal.countDown();
        }
    }

    public void create(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;
        String createdPath = zk.create(path, null/* data */, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.out.println("created path : " + createdPath);
    }

    public void deleteGroup(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;
        if (zk.exists(path, false) != null) {
            List<String> children = zk.getChildren(path, false);
            for (String child : children) {
                System.out.println("Deleted " + path + "/" + child);
                zk.delete(path + "/" + child, -1);
            }
            zk.delete(path, -1);
            System.out.printf("Deleted group %s at path %s\n", groupName, path);
        }
    }

    public void joinGroup(String groupName, String memberName) throws KeeperException, InterruptedException {
        String path = "/" + groupName + "/" + memberName;
        String createdPath = zk.create(path, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        System.out.println("Created " + createdPath);
    }

    public void listGroup(String groupName) throws KeeperException, InterruptedException {
        String path = "/" + groupName;

        List<String> children = zk.getChildren(path, false);
        Collections.sort(children);
        for (String child : children) {
            System.out.println(child);
        }
    }

    public void close() throws InterruptedException {
        zk.close();
    }

    public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
        String host = "";
        String groupName = "";
        CreateGroup createGroup = new CreateGroup();
        createGroup.connect(host);
        createGroup.create(groupName);
        createGroup.close();
    }

}
